#include "window_watch_face.h"
#include "window_main.h"	
#include "window_manager.h"
extern Watch_Face_t  Current_Face;

static lv_obj_t *tv=NULL; //创建titleview对象


// 表盘切换界面
window_manger_t window_watch_face_manger[]/*__attribute__((at(0x23000000)))*/=
{
	[Watch_Face_1 ]={.window_obj=NULL, .col_id=0,.row_id=0,.dir=LV_DIR_HOR,.Init=watch_face_1_switch_init},           								 //下拉菜单
	[Watch_Face_2 ]={.window_obj=NULL, .col_id=1,.row_id=0,.dir=LV_DIR_HOR,.Init=watch_face_2_switch_init},
	[Watch_Face_3 ]={.window_obj=NULL, .col_id=2,.row_id=0,.dir=LV_DIR_HOR,/*.Init=watch_face_3_switch_init*/},
	[Watch_Face_4] ={.window_obj=NULL, .col_id=3,.row_id=0,.dir=LV_DIR_HOR,/*.Init=watch_face_4_switch_init*/},
	[Watch_Face_5] ={.window_obj=NULL, .col_id=4,.row_id=0,.dir=LV_DIR_HOR,/*.Init=watch_face_5_switch_init*/},
	[Watch_Face_custom  ]={.window_obj=NULL, .col_id=5,.row_id=0,.dir=LV_DIR_HOR,/*.Init=watch_face_custom_switch_init*/},                                           
};



void  watch_face_1_event_hander(lv_event_t * e)
{
	
	lv_event_code_t code =lv_event_get_code(e);
  lv_obj_t *  obj= lv_event_get_current_target(e);//  lv_event_get_target(e);
	
	if(code == LV_EVENT_CLICKED)
	{
      printf("%s   Click\r\n",__FUNCTION__);
	    Current_Face=Watch_Face_1;
		  
		 lv_obj_del(tv);
		 lv_obj_del(title_main);
 	   title_main=NULL;
//		 lv_obj_clean(title_main);
//		 delete_watch_face_task();
		 window_manager_init();
//		 lv_obj_clean(window_manger[WINDOW_MAIN].window_obj);
//		 window_manger[WINDOW_MAIN].window_obj=NULL;
//		 window_main_init();
//lv_obj_set_tile_id(title_main,window_manger[WINDOW_MAIN].col_id, window_manger[WINDOW_MAIN].row_id,LV_ANIM_OFF);

		 //lv_event_send(obj,LV_EVENT_DELETE,NULL); //发送删除事件
		
	}else if(code == LV_EVENT_DELETE)
	{
      printf("%s   Delete\r\n",__FUNCTION__);
	    
	}
	
	  
	
}
void  watch_face_2_event_hander(lv_event_t * e)
{
	lv_event_code_t code =lv_event_get_code(e);
  lv_obj_t *  obj= lv_event_get_current_target(e);//  lv_event_get_target(e);
	
	if(code == LV_EVENT_CLICKED)
	{ 
    printf("%s   Click\r\n",__FUNCTION__);
    Current_Face=Watch_Face_3;
		lv_obj_del(tv);
		lv_obj_del(title_main);
   	title_main=NULL;
//		lv_obj_clean(title_main);
//		delete_watch_face_task();
		window_manager_init();
	}
}
/*
    表盘切换
*/
void watch_face_switch()
{
	   
     printf("%s\r\n",__FUNCTION__);
	   tv = lv_tileview_create(lv_scr_act());                 //创建titlewindow
     lv_obj_set_style_bg_color(tv, lv_color_make(0,0,0), 0);//设置背景颜色为黑色
	   lv_obj_set_scrollbar_mode(tv, LV_SCROLLBAR_MODE_OFF);  //取消滚动条显示
	
	#if 0   //静态创建
		 for(int i=Watch_Face_1;i<Watch_Face_custom ;i++)   //创建界面
		{
			window_watch_face_manger[i].window_obj = lv_tileview_add_tile(tv, window_watch_face_manger[i].col_id, window_watch_face_manger[i].row_id,window_watch_face_manger[i].dir);
			find_watch_Display_Handler(i);
		}
	  //设置当前的显示主界面
   lv_obj_set_tile_id(tv,window_watch_face_manger[Watch_Face_1].col_id, window_watch_face_manger[Watch_Face_1].row_id,LV_ANIM_OFF);
 #else   //动态创建
		
lv_obj_t * watch_face_1_obj  = lv_tileview_add_tile(tv, 0, 0,LV_DIR_HOR);
lv_obj_t * watch_face_2_obj  = lv_tileview_add_tile(tv, 1, 0,LV_DIR_HOR);
//lv_obj_t * watch_face_3_obj  = lv_tileview_add_tile(tv, 2, 0,LV_DIR_HOR);
//lv_obj_t * watch_face_4  = lv_tileview_add_tile(tv, 3, 0,LV_DIR_HOR);
//lv_obj_t * watch_face_5  = lv_tileview_add_tile(tv, 4, 0,LV_DIR_HOR);
//lv_obj_t * watch_face_c  = lv_tileview_add_tile(tv, 5, 0,LV_DIR_HOR);
		
		
	lv_obj_t * watch_face_1 = lv_img_create(watch_face_1_obj);
	lv_img_set_src(watch_face_1, &watch_face_1_bg);
  lv_obj_center(watch_face_1);	
			
	lv_obj_add_flag(watch_face_1_obj,LV_OBJ_FLAG_CLICKABLE ); //增加可以点击的属性
	lv_obj_add_state(watch_face_1_obj,LV_STATE_PRESSED);
	lv_obj_add_event_cb(watch_face_1_obj,watch_face_1_event_hander,LV_EVENT_CLICKED,NULL);   //添加对象回调函数
	
		
	//显示表盘字符
	lv_obj_t*  logo = lv_label_create(watch_face_1_obj); 
	lv_label_set_recolor(logo, true);                        /*Enable re-coloring by commands in the text*/
	lv_obj_set_style_text_font(logo, &language_36, 0);
	if(current_language==LANGUAGE_TYPE_CHINESE)
	{
		lv_label_set_text_fmt(logo,"#ffffff 表盘一#");
	}else 
	if(current_language==LANGUAGE_TYPE_ENGLISH)
	{
		lv_label_set_text_fmt(logo,"#ffffff Face One#");
	}
	lv_obj_align(logo, LV_ALIGN_TOP_MID,0,10);
			
		
	lv_obj_t * watch_face_2_bg = lv_img_create(watch_face_2_obj);
	lv_img_set_src(watch_face_2_bg, &watchface_3_bg);
  lv_obj_center(watch_face_2_bg);	
	lv_obj_add_flag(watch_face_2_obj,LV_OBJ_FLAG_CLICKABLE ); //增加可以点击的属性
	lv_obj_add_state(watch_face_2_obj,LV_STATE_PRESSED);
	lv_obj_add_event_cb(watch_face_2_obj,watch_face_2_event_hander,LV_EVENT_CLICKED/* | LV_EVENT_DELETE*/,NULL);   //添加对象回调函数

	
	
	//显示表盘字符
	lv_obj_t*  logo1 = lv_label_create(watch_face_2_obj); 
	lv_label_set_recolor(logo1, true);                        /*Enable re-coloring by commands in the text*/
	lv_obj_set_style_text_font(logo1, &language_36, 0);
	if(current_language==LANGUAGE_TYPE_CHINESE)
	{
		lv_label_set_text_fmt(logo1,"#ffffff 表盘二#");
	}else 
	if(current_language==LANGUAGE_TYPE_ENGLISH)
	{
		lv_label_set_text_fmt(logo1,"#ffffff Face Two#");
	}
	lv_obj_align(logo1, LV_ALIGN_TOP_MID,0,10);
	
	
	
	
		
	
	
	
	
	
	
	#if 0
	lv_obj_t * watch_face_3_bg = lv_img_create(watch_face_3_obj);
	lv_img_set_src(watch_face_3_bg, &watchface_3_bg);
  lv_obj_center(watch_face_3_bg);	
	lv_obj_add_flag(watch_face_3_bg,LV_OBJ_FLAG_CLICKABLE ); //增加可以点击的属性
	lv_obj_add_state(watch_face_3_bg,LV_STATE_PRESSED);
	lv_obj_add_event_cb(watch_face_3_bg,watch_face_2_event_hander,LV_EVENT_CLICKED/* | LV_EVENT_DELETE*/,NULL);   //添加对象回调函数

	
	
	//显示表盘字符
	lv_obj_t*  logo2 = lv_label_create(watch_face_3_obj); 
	lv_label_set_recolor(logo2, true);                        /*Enable re-coloring by commands in the text*/
	lv_obj_set_style_text_font(logo2, &language_36, 0);
	if(current_language==LANGUAGE_TYPE_CHINESE)
	{
		lv_label_set_text_fmt(logo2,"#ffffff 表盘三#");
	}else 
	if(current_language==LANGUAGE_TYPE_ENGLISH)
	{
		lv_label_set_text_fmt(logo2,"#ffffff Face Three#");
	}
	lv_obj_align(logo2, LV_ALIGN_TOP_MID,0,10);
	
	#endif
	
	
	
	
	
	
	
	
	
	
  lv_obj_set_tile_id(tv,0, 0,LV_ANIM_OFF);

 #endif
}



void  watch_face_1_switch_init()
{
	  printf("%s\r\n",__FUNCTION__);
	  lv_obj_t * bg = lv_img_create(window_watch_face_manger[Watch_Face_1].window_obj);
	  lv_img_set_src(bg, &watch_face_1_bg);
    lv_obj_center(bg);
}
void  watch_face_2_switch_init()
{
    printf("%s\r\n",__FUNCTION__);
	  lv_obj_t * bg = lv_img_create(window_watch_face_manger[Watch_Face_2].window_obj);
	  lv_img_set_src(bg, &watch_face_2_bg);
    lv_obj_center(bg);
}
void  watch_face_3_switch_init()
{



}
void  watch_face_4_switch_init()
{



}
void  watch_face_5_switch_init()
{



}

void  watch_face_custom_switch_init()
{


}




static void find_watch_Display_Handler(uint8_t i)  //查找当前的初始化函数
{
 if(((void *)window_watch_face_manger[i].Init) != NULL)
 {
  ((window_manger_t *)&window_watch_face_manger[i])->Init();
 }

}

